﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Microsoft.Win32;
using AgentObjects;
using SimpleUtils;
using SimpleUtils.Win;

namespace SimpleReminder
{
    public partial class frmDemo : frmBase
    {
        public frmDemo()
        {
            InitializeComponent();

            Agent.InitAgentList(cboAgent);

            nudTop.Value = Properties.Settings.Default.AgentTop;
            nudLeft.Value = Properties.Settings.Default.AgentLeft;

            EnableDisableAllButtons(false);
        }

        private void EnableDisableAllButtons(bool enabled)
        {
            btnPlay.Enabled = enabled;
            btnStop.Enabled = enabled;
            btnAuto.Enabled = enabled;
            btnSpeak.Enabled = enabled;
        }

        private IAgentCtlCharacterEx _character;
        const string CharacterID = "DemoCharacter";

        private void LoadAgent()
        {
            SimpleListItem li = cboAgent.SelectedItem as SimpleListItem;
            if (li == null) return;

            string path = li.Value;
            if (path == null || !File.Exists(path))
                return;

            UnloadCharacter();
            try
            {
                axAgent.Characters.Load(CharacterID, path);
                _character = axAgent.Characters[CharacterID];
                _character.LanguageID = 0x409;
                
                lstAnimation.Items.Clear();
                foreach (string aniName in _character.AnimationNames)
                {
                    lstAnimation.Items.Add(aniName);
                }
                lstAnimation.SelectedIndex = 0;

                _character.Show(null);

                if (_character.Balloon.Enabled)
                {
                    _character.Balloon.FontName = "Tahoma";
                    _character.Balloon.FontSize = 10;
                }

                //_agent.Commands.RemoveAll();
                //_agent.Commands.Add("Options",
                //  "&Options", null, null, null);

                SetAgentPosition();
                Agent.SetDefaultBallonStyle(_character);

                EnableDisableAllButtons(true);
            }
            catch (Exception ex)
            {
                SimpleMessage.ShowException(ex);
            }
        }

        bool _settingDisplayPosition = false;
        private void SetDisplayPosition()
        {
            try
            {
                _settingDisplayPosition = true;
                nudTop.Value = _character.Top;
                nudLeft.Value = _character.Left;
            }
            catch
            {
                throw;
            }
            finally
            {
                _settingDisplayPosition = false;
            }
        }

        private void SetAgentPosition()
        {
            if (_settingDisplayPosition) return;

            if (_character != null)
            {
                _character.Left = (short) nudLeft.Value;
                _character.Top = (short) nudTop.Value;
            }
        }

        private void axAgent_Command(object sender, AxAgentObjects._AgentEvents_CommandEvent e)
        {
            //IAgentCtlUserInput ui = (IAgentCtlUserInput)e.userInput;
            //if (ui.Name == "Options")
            //{
            //    axAgent.PropertySheet.Visible = true;
            //}
        }

        private void axAgent_AgentPropertyChange(object sender, EventArgs e)
        {
            
        }

        private void UnloadCharacter()
        {
            Agent.StopCharacter(_character);
            Agent.HideCharacter(_character);
            try
            {
                axAgent.Characters.Unload(CharacterID);
                //Application.DoEvents();
            }
            catch { }
            _character = null;
        }

        private void frmDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.AgentTop = (short)nudTop.Value;
            Properties.Settings.Default.AgentLeft = (short)nudLeft.Value;
            Properties.Settings.Default.Save();

            stopAutoPlay = true;
            UnloadCharacter();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            stopAutoPlay = true;
            Agent.StopCharacter(_character);
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            Agent.StopCharacter(_character);
            int index = lstAnimation.SelectedIndex;
            if (index == -1) return;
            string ani = lstAnimation.Items[index] as string;
            //_character.Play(ani);
            Agent.PlayAnimation(_character, ani);
        }

        private void lstAnimation_DoubleClick(object sender, EventArgs e)
        {
            btnPlay_Click(sender, e);
        }

        private void btnProperty_Click(object sender, EventArgs e)
        {
            axAgent.PropertySheet.Visible = true;
        }

        private void btnSpeak_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtSpeech.Text))
                return;

            try
            {
                _character.Speak(txtSpeech.Text, null);
            }
            catch (Exception ex)
            {
                SimpleMessage.ShowException(ex);
            }
        }

        bool stopAutoPlay = false;
        private void btnAuto_Click(object sender, EventArgs e)
        {
            Agent.StopCharacter(_character);
            stopAutoPlay = false;

            int saveSelectedIndex = lstAnimation.SelectedIndex;

            int i = saveSelectedIndex - 1;

            try
            {
                EnableDisableAllButtons(false);
                btnStop.Enabled = true;

                while (!stopAutoPlay && i != saveSelectedIndex)
                {
                    i = lstAnimation.SelectedIndex;
                    string ani = lstAnimation.Items[i] as string;

                    Agent.PlayAnimation(_character, ani);

                    i++;
                    if (i >= lstAnimation.Items.Count) i = 0;
                    lstAnimation.SelectedIndex = i;
                }
            }
            catch (Exception ex)
            {
                SimpleMessage.ShowException(ex);
            }
            finally
            {
                EnableDisableAllButtons(true);
            }

        }

        private void cboAgent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboAgent.SelectedIndex >= 0)
                LoadAgent();
        }

        private void axAgent_MoveEvent(object sender, AxAgentObjects._AgentEvents_MoveEvent e)
        {
            SetDisplayPosition();
        }

        private void nudTop_ValueChanged(object sender, EventArgs e)
        {
            SetAgentPosition();
        }

        private void nudLeft_ValueChanged(object sender, EventArgs e)
        {
            SetAgentPosition();
        }


    }//end of class
}
